package com.yeban.base.day04.homework;

import java.util.Scanner;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/9/28
 */

//需求2：有一个数列：8,4,2,1,23,344,12，此时从键盘中任意输入一个数据，判断数列中是否包含此数
//解法：先冒泡排序，再折半查找是否包含
public class Test02 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int[] arr = {8, 4, 2, 1, 23, 344, 12};
        System.out.println("请输入要查找的数字：");
        int target = sc.nextInt();

        int len = arr.length;
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - 1 - i; j++) {
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }

        int left = 0;
        int right = len - 1;

        while(left <= right){
            int middle = (left + right) >> 1;
            if(target < arr[middle]){
                right = middle - 1;
            } else if(target > arr[middle]){
                left = middle + 1;
            } else {
                System.out.println("要查找的数字存在！");
                break;
            }
        }

        if(left > right){
            System.out.println("要查找的数字不存在！");
        }
    }
}
